package com.borqs.samples.developer;

import java.util.ArrayList;

import com.borqs.samples.DBProvider;
import com.borqs.samples.developer.hardware.ProcessSwitch;
import com.borqs.samples.developer.hardware.WifiSwitch;

import android.content.ContentValues;
import android.content.Context;

public class DeveloperDataSample {

    private final String WIFI_STATE_KEY = "wifiState";
    private final String SYNC_STATE_KEY = "syncState";
    private final String AIRPLANE_STATE_KEY = "airplaneStateKey";
    private final String GPS_STATE_KEY = "gpsState";

    private final int SAMPLECOUNT = 2;
    private static int currentCount = 1;

    private Context mContext;
    private DeveloperDBAnlaysis mDBAnlaysis;
    private PreferencesUtil mPreferencesUtil;
    private HardwareStatus mHardwareStatus;

    int wifi;
    int gps;
    int sync;
    int airplane;

    public DeveloperDataSample(Context context) {
        mContext = context;
        mDBAnlaysis = new DeveloperDBAnlaysis(context);
        mPreferencesUtil = new PreferencesUtil(context);
        mHardwareStatus = HardwareStatus.getInstance();
    }

    public void callback(int v, int time) {
        if (isToReachTheNumberOf(currentCount)) {
            if (isHardwareStatusChange()) {
                analysisOfData();
                saveDataToDB(createContentValues(v, time));
            }
            saveHardwareStatusToPreferences();
        }
        currentCount++;

    }

    private boolean isToReachTheNumberOf(int currentCount) {
        if ((currentCount % SAMPLECOUNT) == 0) {
            return true;
        }
        return false;
    }

    /**
     * 如果返回的分析不为空，就关闭最高项，如果为空，则依次关闭模块
     */
    private void analysisOfData() {
        ArrayList<?> anlaysisData = mDBAnlaysis.getConclusiveData();
        if (null != anlaysisData) {

        } else {
            if (mHardwareStatus.getTaskCount() == 0) {
                addTask();
            }
            mHardwareStatus.commit();
        }
    }

    public void addTask() {
        WifiSwitch wifiTask = new WifiSwitch(WifiSwitch.CLOSE, null);
        ProcessSwitch pricessTask = new ProcessSwitch(ProcessSwitch.CLOSE, null);
        mHardwareStatus.addTask(wifiTask);
        mHardwareStatus.addTask(pricessTask);
    }

    public boolean saveDataToDB(ContentValues contentValues) {
        mContext.getContentResolver().insert(
                DBProvider.DeveloperTable.CONTENT_URI, contentValues);
        return true;
    }

    public boolean saveHardwareStatusToPreferences() {
        mPreferencesUtil.putInt(WIFI_STATE_KEY, wifi);
        mPreferencesUtil.putInt(GPS_STATE_KEY, gps);
        mPreferencesUtil.putInt(SYNC_STATE_KEY, sync);
        mPreferencesUtil.putInt(AIRPLANE_STATE_KEY, airplane);
        return true;
    }

    public boolean isHardwareStatusChange() {
        int oldWifi = mPreferencesUtil.getInt(WIFI_STATE_KEY);
        int oldGps = mPreferencesUtil.getInt(GPS_STATE_KEY);
        int oldSync = mPreferencesUtil.getInt(SYNC_STATE_KEY);
        int oldAirplane = mPreferencesUtil.getInt(AIRPLANE_STATE_KEY);

        int wifi = HardwareStatus.getWifiState(mContext);
        int gps = HardwareStatus.getGPSState(mContext);
        int sync = HardwareStatus.getSyncState();
        int airplane = HardwareStatus.getAirplaneState(mContext);

        if (oldWifi != wifi || oldGps != gps || oldSync != sync
                || oldAirplane != airplane) {
            return false;
        }
        return true;
    }

    public ContentValues createContentValues(int v, int time) {

        wifi = HardwareStatus.getWifiState(mContext);
        gps = HardwareStatus.getGPSState(mContext);
        sync = HardwareStatus.getSyncState();
        airplane = HardwareStatus.getAirplaneState(mContext);

        ContentValues cv = new ContentValues();
        cv.put(DBProvider.DeveloperTable.WIFI_SATE, wifi);
        cv.put(DBProvider.DeveloperTable.GPS_SATE, gps);
        cv.put(DBProvider.DeveloperTable.AIRPLANE_STATE, airplane);
        cv.put(DBProvider.DeveloperTable.SYNC_SATE, sync);
        cv.put(DBProvider.DeveloperTable.TIME, time);
        cv.put(DBProvider.DeveloperTable.START_VELOCITY, v);
        return cv;
    }
}
